package cn.bluech.example.ui

import android.graphics.Color
import android.os.Bundle
import android.view.View
import cn.bluech.example.R
import cn.bluech.example.ui.widget.WeatherPanelView
import cn.bluech.lib.ui.widget.SlidingUpPanelLayout
import cn.bluech.lib.ui.widget.SlidingUpPanelLayout.ISlidingUpPanel
import cn.bluech.lib.ui.widget.SlidingUpPanelLayout.PanelSlideListenerAdapter
import cn.bluech.lib.utils.DispUtils
import kotlinx.android.synthetic.main.activity_weather.*

/**
 * Description:
 * Create by Codeant on 2019/12/16.
 */
class WeatherActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_weather)
        weatherLy.setPanelSlideListener(object : PanelSlideListenerAdapter() {
            override fun onPanelExpanded(panel: ISlidingUpPanel<out View>) {
                if (panel is WeatherPanelView) {
                    val count: Int = weatherLy.getChildCount()
                    // 如果被展开的Panel不是距离屏幕顶部最近（floor值最大）那个，做如下处理，再被收起时已是距屏幕顶部最近
                    if (panel.floor != count - 1) {
                        weatherLy.removeView(panel.getPanelView())
                        weatherLy.addView(panel.getPanelView(), 1)
                        for (i in 1 until count) {
                            val child = weatherLy.getChildAt(i) as WeatherPanelView
                            child.floor = count - i
                        }
                        weatherLy.requestLayout()
                    }
                }
                val count: Int = weatherLy.getChildCount()
                for (i in 1 until count) {
                    val panel2 = weatherLy.getChildAt(i) as ISlidingUpPanel<out View>
                    if (panel2 === panel) {
                        panel2.panelView.isEnabled = false
                    } else {
                        panel2.slideState = SlidingUpPanelLayout.HIDDEN
                        panel2.panelView.isEnabled = true
                    }
                }
            }

            override fun onPanelCollapsed(panel: ISlidingUpPanel<out View>) {
                val count: Int = weatherLy.getChildCount()
                for (i in 1 until count) {
                    val p = weatherLy.getChildAt(i) as ISlidingUpPanel<out View>
                    p.slideState = SlidingUpPanelLayout.COLLAPSED
                    p.panelView.isEnabled = true
                }
            }
        })

//        int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
//        int statusBarHeight = getResources().getDimensionPixelSize(resId);
//        mBgLayout.setPadding(0, statusBarHeight, 0, 0);
        loadData()
    }

    private fun loadData() {
        weatherLy.setAdapter(object : SlidingUpPanelLayout.Adapter() {
            private val mSize = 3
            override fun getItemCount(): Int {
                return mSize
            }

            override fun onCreateSlidingPanel(position: Int): ISlidingUpPanel<out View> {
                val panel = WeatherPanelView(this@WeatherActivity)
                panel.floor = mSize - position
                panel.setPanelHeight(if (mSize == 1) DispUtils.dp2px(baseContext, 120f) else DispUtils.dp2px(baseContext, 80f))
                if (position == 0) {
                    panel.slideState = SlidingUpPanelLayout.EXPANDED
                    panel.isEnabled = false
                } else {
                    panel.slideState = SlidingUpPanelLayout.HIDDEN
                    panel.isEnabled = true
                }
                return panel
            }

            override fun onBindView(panel: ISlidingUpPanel<*>, position: Int) {
                val view = panel.panelView
                if (position == 0) {
                    view.setBackgroundColor(Color.parseColor("#80DEEA"))
                } else if (position == 1) {
                    view.setBackgroundColor(Color.parseColor("#78909C"))
                } else {
                    view.setBackgroundColor(Color.parseColor("#03A9F4"))
                }
                view.isClickable = true
                view.setOnClickListener {
                    if (panel.slideState != SlidingUpPanelLayout.EXPANDED) {
                        weatherLy.expandPanel()
                    } else {
                        weatherLy.collapsePanel()
                    }
                }
            }
        })
    }
}